Add-Type -As System.IO.Compression.FileSystem
 
# Copy MSI files to bin folder
Invoke-Expression "$Env:TF_BUILD_SOURCESDIRECTORY\BuildScripts\GatherItemsForDrop.ps1"

$destinationFolder = $Env:TF_BUILD_BINARIESDIRECTORY
cd $destinationFolder

# Create destination folder
Write-Output ""
$destFolder = $Env:TF_BUILD_BUILDNUMBER
Write-Output "Creating destination folder: " $destFolder
Remove-Item $destFolder -Recurse -Force -ErrorAction SilentlyContinue
New-Item -ItemType directory -Path $destFolder

# Copy msi files
Write-Output "Copying msi files"
$applications = Get-Content -Path "$Env:TF_BUILD_SOURCESDIRECTORY\BuildScripts\Applications.txt"

ForEach ($app in $applications)
{
  Write-Output $app
  Copy-Item -Path $app*.msi -Destination $destFolder
}

# Copy deploy scripts
Write-Output ""
Write-Output "Copying deploy scripts"

Copy-Item -Path "$Env:TF_BUILD_SOURCESDIRECTORY\BuildScripts\PowerShellModules\DeployTools\bin\*\DeployTools.dll" -Destination $destFolder
Copy-Item -Path "$Env:TF_BUILD_SOURCESDIRECTORY\BuildScripts\Deploy.ps1" -Destination $destFolder
Copy-Item -Path "$Env:TF_BUILD_SOURCESDIRECTORY\BuildScripts\Undeploy.ps1" -Destination $destFolder
Copy-Item -Path "$Env:TF_BUILD_SOURCESDIRECTORY\BuildScripts\Install-BizTalkApplication.ps1" -Destination $destFolder
Copy-Item -Path "$Env:TF_BUILD_SOURCESDIRECTORY\BuildScripts\AutoDeploy.ps1" -Destination $destinationFolder
Copy-Item -Path "$Env:TF_BUILD_SOURCESDIRECTORY\BuildScripts\Applications.txt" -Destination $destinationFolder


# Create app list
$filePath = Join-Path -Path $destFolder -ChildPath "Applications.txt"
$applications | Out-File -FilePath $filePath

# Create ZIP file
$source = Join-Path $destinationFolder -ChildPath $destFolder
$dest = Join-Path $destinationFolder -ChildPath "$destFolder.zip"
[IO.Compression.ZipFile]::CreateFromDirectory( $source, $dest, "Optimal", $false )
Remove-Item $source -Recurse -Force -ErrorAction SilentlyContinue